import { defineStore } from 'pinia';
import { ref, computed } from 'vue';
import { McpService } from '../types';
import apiClient from '../utils/api';

export const useMcpServiceStore = defineStore('mcpService', () => {
  const mcpServices = ref<McpService[]>([]);
  const isLoading = ref(false);
  const error = ref<string | null>(null);
  const selectedMcpService = ref<McpService | null>(null);

  // 计算属性
  const activeMcpServices = computed(() => 
    mcpServices.value.filter(service => service.is_active)
  );

  // 获取MCP服务列表
  const fetchMcpServices = async () => {
    try {
      isLoading.value = true;
      error.value = null;
      
      const data = await apiClient.getMcpServices();
      mcpServices.value = data;
      
      return true;
    } catch (err: any) {
      error.value = err.response?.data?.detail || '获取MCP服务列表失败';
      return false;
    } finally {
      isLoading.value = false;
    }
  };

  // 创建MCP服务
  const createMcpService = async (data: { name: string; description: string; server_url: string }) => {
    try {
      isLoading.value = true;
      error.value = null;
      
      const newService = await apiClient.createMcpService(data);
      mcpServices.value.unshift(newService);
      
      return true;
    } catch (err: any) {
      error.value = err.response?.data?.detail || '创建MCP服务失败';
      return false;
    } finally {
      isLoading.value = false;
    }
  };

  // 更新MCP服务
  const updateMcpService = async (id: number, data: { name: string; description: string; server_url: string; is_active: boolean }) => {
    try {
      isLoading.value = true;
      error.value = null;
      
      const updatedService = await apiClient.updateMcpService(id, data);
      const index = mcpServices.value.findIndex(service => service.id === id);
      if (index !== -1) {
        mcpServices.value[index] = updatedService;
      }
      
      return true;
    } catch (err: any) {
      error.value = err.response?.data?.detail || '更新MCP服务失败';
      return false;
    } finally {
      isLoading.value = false;
    }
  };

  // 删除MCP服务
  const deleteMcpService = async (id: number) => {
    try {
      isLoading.value = true;
      error.value = null;
      
      await apiClient.deleteMcpService(id);
      mcpServices.value = mcpServices.value.filter(service => service.id !== id);
      
      if (selectedMcpService.value?.id === id) {
        selectedMcpService.value = null;
      }
      
      return true;
    } catch (err: any) {
      error.value = err.response?.data?.detail || '删除MCP服务失败';
      return false;
    } finally {
      isLoading.value = false;
    }
  };

  // 选择MCP服务
  const selectMcpService = (service: McpService | null) => {
    selectedMcpService.value = service;
  };

  // 清除错误
  const clearError = () => {
    error.value = null;
  };

  return {
    mcpServices,
    isLoading,
    error,
    selectedMcpService,
    activeMcpServices,
    fetchMcpServices,
    createMcpService,
    updateMcpService,
    deleteMcpService,
    selectMcpService,
    clearError,
  };
});